package com.wwl.cms.config.elasticsearch;

import com.alibaba.fastjson.JSON;
import com.wwl.cms.basic.PagedOutput;
import com.wwl.cms.dto.AuditPoiVo;
import com.wwl.cms.dto.AuditPoiVoReqVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/11/29 16:44
 * @description:
 */
@Slf4j
public class EsApplication {
    private String indexName ="audit_poi";
    @Resource
    private RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(new HttpHost("localhost", 9200, "http")));



    public PagedOutput<AuditPoiVo> findByPage(AuditPoiVoReqVo auditPoiVoReqVo) {
        try {
            SearchRequest request = new SearchRequest();
            request.indices(indexName);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.from((auditPoiVoReqVo.getPageNum() - 1) * auditPoiVoReqVo.getPageSize());
            sourceBuilder.size(auditPoiVoReqVo.getPageSize());
            sourceBuilder.sort("createTime", SortOrder.DESC);
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            if (StringUtils.hasText(auditPoiVoReqVo.getName())) {
                boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("name", auditPoiVoReqVo.getName()));
            }
            if (null != auditPoiVoReqVo.getCountryId()) {
                boolQueryBuilder.must(QueryBuilders.termQuery("countryId", auditPoiVoReqVo.getCountryId()));
            }
            if (null != auditPoiVoReqVo.getCityId()) {
                boolQueryBuilder.must(QueryBuilders.termQuery("cityId", auditPoiVoReqVo.getCityId()));
            }
            if (StringUtils.hasText(auditPoiVoReqVo.getSiteType())) {
                boolQueryBuilder.must(QueryBuilders.termQuery("siteType", auditPoiVoReqVo.getSiteType()));
            }
            if (null != auditPoiVoReqVo.getTarget() && 0 != auditPoiVoReqVo.getTarget()) {
                boolQueryBuilder.must(QueryBuilders.termQuery("target", auditPoiVoReqVo.getTarget()));
            }
            if (null != auditPoiVoReqVo.getTargetId()) {
                boolQueryBuilder.must(QueryBuilders.termQuery("targetId", auditPoiVoReqVo.getTargetId()));
            }
            if (null != auditPoiVoReqVo.getDataSources() && 0 != auditPoiVoReqVo.getDataSources()) {
                boolQueryBuilder.must(QueryBuilders.termQuery("dataSources", auditPoiVoReqVo.getDataSources()));
            }
            if (null != auditPoiVoReqVo.getStatus() && 0 != auditPoiVoReqVo.getStatus()) {
                boolQueryBuilder.must(QueryBuilders.termQuery("status", auditPoiVoReqVo.getStatus()));
            }
            if (null != auditPoiVoReqVo.getOperateType() && 0 != auditPoiVoReqVo.getOperateType()) {
                boolQueryBuilder.must(QueryBuilders.termQuery("operateType", auditPoiVoReqVo.getOperateType()));
            }
            if (null != auditPoiVoReqVo.getCreateTimeSta() && null != auditPoiVoReqVo.getCreateTimeEnd()) {
                boolQueryBuilder.must(QueryBuilders.rangeQuery("createTime").gte(auditPoiVoReqVo.getCreateTimeEnd()).lte(auditPoiVoReqVo.getCreateTimeSta()));
            }
            sourceBuilder.query(boolQueryBuilder);
            request.source(sourceBuilder);

            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            SearchHits searchHits = response.getHits();
            List<AuditPoiVo> list = CommonQueryUtils.parseResponse(response);
            return new PagedOutput(auditPoiVoReqVo.getPageSize(), auditPoiVoReqVo.getPageNum(), searchHits.getTotalHits().value, list);
        } catch (IOException e) {
            log.info("返回结果:{}", e.getMessage());
        }
        return new PagedOutput(auditPoiVoReqVo.getPageSize(), auditPoiVoReqVo.getPageNum(), 0, null);
    }


    /**
     * 添加编辑接口
     *
     * @param auditPoiVo
     * @return
     */
    public void saveAuditPoi(AuditPoiVo auditPoiVo) {
        if (null != auditPoiVo && null != auditPoiVo.getId()) {
            try {
                IndexRequest request = new IndexRequest(indexName);
                request.id(auditPoiVo.getId().toString());
                request.source(JSON.toJSONString(auditPoiVo), XContentType.JSON);
                client.index(request, RequestOptions.DEFAULT);
            } catch (IOException e) {
                log.info("链接查询es失败:{}！", e.getMessage());
            }

        }
    }

}
